//
//  NotesCommentTableViewController.m
//  TravelFirst
//
//  Created by lanou on 15/9/24.
//  Copyright (c) 2015年 吴娅君. All rights reserved.
//

#import "NotesCommentTableViewController.h"
#import "NotesDetailTableViewCell.h"
#import "NotesCommentTableViewCell.h"

@interface NotesCommentTableViewController ()

@property (nonatomic, strong) NSMutableArray *commentArray;
@property (nonatomic, strong) NotesModel *notesModel;

@end

@implementation NotesCommentTableViewController


#pragma mark - 加载数据 -

// 介绍信息
- (void)reloadDescription
{
    NSString *urlStr = [kNotesPictureDescription stringByReplacingOccurrencesOfString:@"参数" withString:self.posts_id];
    [LORequestManger GET:urlStr success:^(id response) {
        
        NSDictionary *dic = (NSDictionary *)response;
        [self.notesModel shareJsonWithDictionary:dic[@"post"]];
        [self.notesModel shareJsonWithDictionary:dic[@"post"][@"created_by"]];
        self.notesModel.picture = dic[@"post"][@"pictures"][0][@"picture"];
        
        [self reloadComment];
        
        [self.tableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@", error);
    }];
}

// 评论信息
- (void)reloadComment
{
    NSString *urlStr = [kNotesPictureComment stringByReplacingOccurrencesOfString:@"参数" withString:self.posts_id];
    [LORequestManger GET:urlStr success:^(id response) {

        NSDictionary *dic = (NSDictionary *)response;
        BOOL isLoad = dic[@"comments"] && [dic[@"comments"] count] != 0;
        if (isLoad) {
            for (NSDictionary *commentsDic in dic[@"comments"]) {
                NotesModel *notesModel = [[NotesModel alloc] init];
                [notesModel shareJsonWithDictionary:commentsDic];
                [notesModel shareJsonWithDictionary:commentsDic[@"created_by"]];
                notesModel.total = dic[@"total"];
                
                [self.commentArray addObject:notesModel];
            }
            [self.tableView reloadData];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@", error);
    }];
}


#pragma mark - viewDidLoad -

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self reloadDescription];
    
    self.navigationItem.leftBarButtonItem.title = nil;
    
    self.navigationController.navigationBar.alpha = 1;
    self.navigationItem.title = @"评论";
    
    [self.tableView registerClass:[NotesDetailTableViewCell class] forCellReuseIdentifier:@"NotesDetailTableViewCell"];
    [self.tableView registerClass:[NotesCommentTableViewCell class] forCellReuseIdentifier:@"NotesCommentTableViewCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = kColor;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Table view data source -

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else
    return [self.commentArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        NotesDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NotesDetailTableViewCell" forIndexPath:indexPath];
        [cell setValueWithModel:self.notesModel string:@"NotesCommentTableViewController"];
        return cell;
    } else {
        
        NotesCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NotesCommentTableViewCell" forIndexPath:indexPath];
        if (self.commentArray && self.commentArray.count != 0) {
            NotesModel *notesModel = self.commentArray[indexPath.row];
            [cell setValueWithModel:notesModel];
        }
        
        return cell;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        //NotesModel *notesModel = self.notesModel;
        CGFloat height = 0;
        if (self.notesModel.posts_description && self.notesModel.posts_description.length != 0) {
            height = [NotesDetailTableViewCell setFrameWithString:self.notesModel.posts_description].size.height + 10;
        }
        
        return (225 + height + 35);
    } else {
        
        if (self.commentArray && self.commentArray.count != 0) {
            NotesModel *notesModel = self.commentArray[indexPath.row];
            return [NotesCommentTableViewCell appendStringWithModel:notesModel] + 26;
        }
        return 56;
    }
    
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        if (self.commentArray.count > 0) {
            UIView *view = [[UIView alloc] init];
            view.backgroundColor = kColor;
            
            UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, kWidth - 20, 30)];
            backgroundView.backgroundColor = [UIColor whiteColor];
            [view addSubview:backgroundView];
            
            UILabel *commentLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 50, 20)];
            commentLabel.text = @"评论";
            [backgroundView addSubview:commentLabel];
            
            UILabel *totalLabel = [[UILabel alloc] initWithFrame:CGRectMake(kWidth - 20 - 100 - 10, 5, 100, 20)];
            totalLabel.text = [NSString stringWithFormat:@"%ld条", self.commentArray.count];
            totalLabel.textAlignment = NSTextAlignmentRight;
            [backgroundView addSubview:totalLabel];
            return view;
        }
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        return 41;
    }
    return 0;
}


#pragma mark - 懒加载 -

- (NSMutableArray *)commentArray
{
    if (!_commentArray) {
        _commentArray = [[NSMutableArray alloc] init];
    }
    return _commentArray;
}

- (NotesModel *)notesModel
{
    if (!_notesModel) {
        _notesModel = [[NotesModel alloc] init];
    }
    return _notesModel;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
